package com.kang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.bean.AccessToken;
import com.kang.bean.Menus;
import com.kang.bean.RoleMenus;
import com.kang.commons.util.EncryptUtil;
import com.kang.mapper.MenusMapper;
import com.kang.mapper.RoleMenusMapper;
import com.kang.service.MenusService;
import com.kang.vo.MenusRoleVo;
import com.kang.vo.MenusVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: service
 * @description:
 * @author: K.faWu
 * @create: 2022-02-28 14:14
 **/
@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus> implements MenusService {
    @Autowired
    private MenusMapper menusMapper;
    @Autowired
    private RoleMenusMapper roleMenusMapper;


    @Override
    public List<MenusVo> getAll(HttpServletRequest request) {
        String token = request.getHeader("token");
        // 解析token
        String jsonToken = EncryptUtil.decrypt(token);
        AccessToken accessToken = JSONObject.parseObject(jsonToken, AccessToken.class);
        // 获取用户名，验证用户名
        Long userId = accessToken.getUserId();

        List<MenusVo> menus = menusMapper.getListByUserId(userId);
        // 顶级菜单
        ArrayList<MenusVo> top = new ArrayList<>();
        for(Menus m:menus){
            if(m.getPid() == -1){
                //将最高级菜单装进集合
                MenusVo menusVo = new MenusVo();
                BeanUtils.copyProperties(m, menusVo);
                top.add(menusVo);
            }
        }
        //将次级菜单装进顶级菜单中
        for(MenusVo t:top){
            setChildren(t,menus);
        }
        return top;
    }

    public List<MenusVo> getAll() {
        List<MenusVo> menus = menusMapper.getAll();
        // 顶级菜单
        ArrayList<MenusVo> top = new ArrayList<>();
        for(Menus m:menus){
            if(m.getPid() == -1){
                //将最高级菜单装进集合
                MenusVo menusVo = new MenusVo();
                BeanUtils.copyProperties(m, menusVo);
                top.add(menusVo);
            }
        }
        //将次级菜单装进顶级菜单中
        for(MenusVo t:top){
            setChildren(t,menus);
        }
        return top;
    }

    @Override
    public void add(Menus menus) {
        menusMapper.add(menus);
        RoleMenus roleMenus = new RoleMenus();
        roleMenus.setMenusId(menus.getId());
        roleMenus.setRoleCode("root_admin");
        roleMenusMapper.insert(roleMenus);
    }

    @Override
    public void update(Menus menus) {
        menusMapper.updateById(menus);
    }

    @Override
    public MenusRoleVo getByRoleCode(String roleCode) {
        MenusRoleVo menusRoleVo = new MenusRoleVo();
        List<MenusVo> list = getAll();
        menusRoleVo.setList(list);

        List<String> idList = new ArrayList<>();
        List<Long> longList = roleMenusMapper.getMenusIdByCode(roleCode);
        getIdList(list, idList,longList);
        menusRoleVo.setIdList(idList);
        return menusRoleVo;
    }

    @Override
    public void delete(List<MenusVo> menusVos) {
        List<MenusVo> list = new ArrayList<>();
        for (MenusVo menusVo: menusVos){
            list = analysisRoot(menusVo, list);
        }
        menusMapper.deleteByList(list);
        roleMenusMapper.deleteByMenusId(list);
    }

    public List<MenusVo> analysisRoot(MenusVo root, List<MenusVo> menusVoList){
        menusVoList.add(root);
        List<MenusVo> children = root.getChildren();
        if (children!= null && children.size()>0){
            for (MenusVo menusVo: children){
                analysisRoot(menusVo, menusVoList);
            }
        }
        return menusVoList;
    }

    private void getIdList(List<MenusVo> list, List<String> idList, List<Long> longList) {
        if (list != null && list.size() != 0){
            for (MenusVo menusVo: list){
                List<MenusVo> children = menusVo.getChildren();
                if (children != null && children.size() != 0){
                    getIdList(children,idList,longList);
                }else{
                    if (longList.remove(menusVo.getId())) {
                        idList.add(menusVo.getId().toString());
                    }
                }
            }
        }
    }

    public void setChildren(MenusVo top, List<MenusVo> menus) {
        List<MenusVo> childs = new ArrayList<>();

        if (menus!=null &&  menus.size()>0){
            for(Iterator<MenusVo> it = menus.iterator(); it.hasNext();){
                Menus m = it.next();
                //菜单的父id等于top的id，说明是top的次级菜单
                if(m.getPid().equals(top.getId())){
                    MenusVo menusVo = new MenusVo();
                    BeanUtils.copyProperties(m, menusVo);
                    childs.add(menusVo);
                    it.remove();
                }
            }
            top.setChildren(childs);
            if(childs.size() > 0){
                for(MenusVo child : childs){
                    this.setChildren(child, menus);
                }
            }
        }


    }
}
